package com.dishdex.menuhub.console.controller;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.dishdex.menuhub.console.domain.dish.DishExportVO;
import com.dishdex.menuhub.module.module.dish.entity.Dish;
import com.dishdex.menuhub.module.module.dish.service.DishService;
import com.dishdex.menuhub.module.utils.BaseUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName:
 * Package: com.dishdex.menuhub.console.controller
 * Description:
 *
 * @Author:
 * @Create 2025/11/4 15:29
 * @Version 1.0
 */
public class DishImportListener extends AnalysisEventListener<DishExportVO> {

    private DishService dishService;
    private List<Dish> dishList = new ArrayList<>();
    private static final int BATCH_COUNT = 200;

    public DishImportListener(DishService dishService) {
        this.dishService = dishService;
    }

    @Override
    public void invoke(DishExportVO vo, AnalysisContext context) {
        Dish dish = new Dish();
        dish.setDishName(vo.getDishName());
        dish.setDishDescription(vo.getDishDescription());
        dish.setDishScore(vo.getDishScore());
        dish.setDishPrice(vo.getDishPrice());
        dish.setDishIngredients(vo.getDishIngredients());
        dish.setDishReview(vo.getDishReview());
        dish.setCoverImages(vo.getCoverImages());
        dish.setCreateTime(BaseUtils.currentSeconds());
        dish.setUpdateTime(BaseUtils.currentSeconds());
        dish.setIsDeleted(vo.getIs_deleted() == null ? 0 : vo.getIs_deleted());
        dish.setCategoryId(vo.getCategoryId());

        dishList.add(dish);

        if (dishList.size() >= BATCH_COUNT) {
            dishService.saveBatch(dishList);
            dishList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (!dishList.isEmpty()) {
            dishService.saveBatch(dishList);
            dishList.clear();
        }
    }

}
